import {defineStore} from 'pinia'
import { formatDateToLocal } from '@/utils/date.js'



export const useBlogsStore = defineStore('blogs',{
  state:()=>{
    return {
      blogs: [
        {
          id: 1,
          title: '理解现代Type Scrip框架',
          author: 'John Doe',
          date: '2024-05-28',
          content: '在这篇文章中，我们将深刻的理解Type Script',
          comments:[
            {author:'张三',date:'2024-05-29',content:'感谢分享,受益匪浅'},
            {author:'王敏',date:'2024-05-30',content:'一直对Vue很感兴趣'}
          ]
        },
        {
          id: 2,
          title: '微服务架构的优缺点',
          author: '王强',
          date: '2024-05-25',
          content: '本文介绍了微服务架构的优点和缺点，优点包括，拆分了在不同场景下的基本概念，缺点是，并分析了在不同场景下的适用性。',
          comments:[
            {author:'刘敏',date:'2024-05-26',content:'感谢分享,受益匪浅'},
            {author:'赵丽',date:'2024-05-27',content:'一直对微服务很感兴趣'}
          ]
        }
      ]
    }
  },
  getters: {
    sorted: state => {
      return state.blogs.sort((a,b)=>new Date(b.date)-new Date(a.date))
    }
  },
  actions:{
    addBlog(blog){
      this.blogs.push({
        id:this.blogs.length+1,
        title:blog.title,
        author:'万行',
        // date:new Date().toISOString().replace('T', ' ').split('.')[0],
        date:formatDateToLocal(), // 使用工具函数格式化日期,
        content:blog.content,
      });
    },
    getBlogById(id){
      return this.blogs.find(blog=>blog.id===id);
    }
  }
});
